<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { reactive, watch, inject, Ref, ref, onBeforeUnmount, onMounted } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { Row } from '@/api/system/staff'
import { searchUserCateApi } from '@/api/utils/search'
const { required } = useValidator()
const currentRow: Ref<Row> = inject('currentRow') as Ref<Row>
const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods
const options = ref([])
const getSearchSupplierCateApi = async () => {
  const res = await searchUserCateApi({ field: 'name,id' })
  options.value = res.data.list
}
onMounted(() => {
  getSearchSupplierCateApi()
})
const schema = reactive<FormSchema[]>([
  {
    field: 'user_name',
    label: '登录账号',
    component: 'Input',
    formItemProps: {
      rules: [
        required(),
        { min: 6, max: 20, message: '请输入至少6字符最多支持20字符', trigger: 'blur' }
      ]
    },
    componentProps: {
      placeholder: '输入6-20个字符'
    }
  },
  {
    field: 'mobile',
    label: '手机号',
    component: 'Input',
    formItemProps: {
      rules: [
        required(),
        { min: 11, max: 11, message: '请输入手机号', trigger: 'blur' },
        {
          pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
          message: '请输入手机号',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      placeholder: '请输入手机号'
    }
  },
  {
    field: 'password',
    label: '设置密码',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 6, max: 15, message: '输入6-15个字符', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '输入6-15个字符'
    }
  },
  {
    field: 'role_ids',
    label: '选择角色',
    component: 'Select',
    formItemProps: {
      rules: [{ required: true, message: '请选择角色', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请选择',
      options: [],
      multiple: true
      // slots: {
      //   default: (options) => {
      //     if (options.length) {
      //       return options?.map((v) => {
      //         return <ElOption key={v.id} label={v.name} value={v.id} />
      //       })
      //     } else {
      //       return null
      //     }
      //   }
      // }
    },
    optionApi: async () => {
      const res = await searchUserCateApi({ field: 'name,id' })
      if (res) {
        return res.data.list.map((item) => {
          return {
            label: item.name,
            value: item.id
          }
        })
      }
    }
  }
  // {
  //   field: 'role_ids',
  //   label: '选择角色',
  //   component: 'Select',
  //   formItemProps: {
  //     rules: [{ required: true, message: '请选择角色', trigger: 'blur' }]
  //   },
  //   componentProps: {
  //     placeholder: '请选择',
  //     options: [],
  //     multiple: true
  //   },
  //   optionApi: async () => {
  //     const res = await searchUserCateApi({ field: 'name,id' })
  //     if (res) {
  //       return res.data.list.map((item) => {
  //         return {
  //           label: item.name,
  //           value: item.id
  //         }
  //       })
  //     }
  //   }
  // }
])

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    const formDataList = {
      id: formData.id,
      role_ids: formData.role_ids.join(','),
      password: formData.password,
      mobile: formData.mobile,
      user_name: formData.user_name
    }
    return formDataList
  }
}

const reset = async () => {
  const elForm = await getElFormExpose()
  elForm?.resetFields()
}

watch(
  () => currentRow.value,
  (currentRow) => {
    if (!currentRow) return
    if (typeof currentRow.role_ids === 'string') {
      currentRow.role_ids = currentRow.role_ids.split(',').map(Number)
    }
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

onBeforeUnmount(reset)

defineExpose({
  submit,
  reset
})
</script>
<template>
  <Form @register="formRegister" :schema="schema" />
</template>
<style scoped lang="less">
:deep(.el-tag) {
  mask: unset;
  width: auto !important;
}
</style>
